﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.IO;
using QLab.UX.Engine;

namespace QLab.UX.Forms
{
    public partial class FunctionEditor : DockContent
    {
        private QLabFunction function;
        private string functionText;

        public QLabFunction Function
        {
            get { return this.function; }
        }

        public FunctionEditor(QLabFunction function)
        {
            InitializeComponent();
            this.function = function;
            this.Text = this.function.Name;
            this.richTextBoxFunction.Text = this.function.Text;
            this.function.FunctionSaved += new QLabFunction.FunctionSavedEventHandler(function_FunctionSaved);
            this.function.FunctionClosing += new QLabFunction.FunctionClosingEventHandler(function_FunctionClosing);
        }

        void function_FunctionClosing(object sender, FunctionSavedEventArgs e)
        {
            this.Close();
        }

        void function_FunctionSaved(object sender, FunctionSavedEventArgs e)
        {
            this.Text = this.function.Name;
        }

        private void richTextBoxFunction_TextChanged(object sender, EventArgs e)
        {
            this.Text = this.function.Name + "*";
            this.function.SetText(this.richTextBoxFunction.Text);
        }

        private void richTextBoxFunction_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.S)
                this.function.Save();
        }

        protected override string GetPersistString()
        {
            return EnumsEngine.GetToolWindowName(ToolWindowType.FunctionEditor);
        }
    }
}
